//
//  AttentionViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/28.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "AttentionViewController.h"
#import "HX_Macro.h"
#import "HXAttentionTCell.h"
#import "HXAttentViewModel.h"
#define kTableView @"reuseTableView"
@interface AttentionViewController ()<UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrAll;
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation AttentionViewController

- (UIWebView *)webView
{
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
        _webView.dataDetectorTypes = UIDataDetectorTypeAll;
    }
    return _webView;
}
- (NSMutableArray *)arrAll {
    if (!_arrAll) {
        _arrAll = [NSMutableArray array];
    }
    return _arrAll;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getNetWorking];
    [self creatTableView];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"HXAttentionTCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableView];
}

//- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    
//    NSString *jsString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"article" ofType:@"js"] encoding:NSUTF8StringEncoding error:nil];
//    [self.articleWebView stringByEvaluatingJavaScriptFromString:jsString];
//#warning 计算webView的内容高度 通过返回webView的内容高度来确定footerView的位置
//    self.webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
//    
//    
//    //    if (self.webViewHeight != 0) {
//    //        self.articleWebView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, footerViewHeight, 0);
//    //        self.footerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.webViewHeight, SCREEN_WIDTH, footerViewHeight)];
//    //        [self setupFooterView];
//    //        self.footerView.backgroundColor = [UIColor greenColor];
//    //        NSLog(@"%f", self.webViewHeight);
//    //        [self.articleWebView.scrollView addSubview:self.footerView];
//    //    }
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrAll.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXAttentionTCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView];
    HXAttentViewModel *attentView = self.arrAll[indexPath.row];
    cell.attentView = attentView;
    [self.webView loadHTMLString:attentView.content baseURL:nil];
    NSLog(@"%@", attentView.content);
    cell.backgroundColor = COLORAUTO;
    return cell;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}
- (void)getNetWorking {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"page=1&methodName=ShequFollow&size=20&version=4.40&";
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dic in arrData) {
            HXAttentViewModel *att = [HXAttentViewModel ModelWithDic:dic];
            [self.arrAll addObject:att];
        }
        [_tableView reloadData];
    } Failure:^(NSError *error) { 
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 670;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
